let userDetail = document.querySelector('.user-details');
let userStr = ''
let myModal = document.querySelector('#myModal')
let modalContent = document.querySelector('.modal-content')

function closeModal() {
    myModal.style.display = 'none'
}

getUserInfo()

function getUserInfo() {
    axios.get('/api/getuserall', {
        headers: {
            'Authorization': localStorage.getItem('token')
        }
    }).then(res => {
        res.data.data.forEach(data => {
            userStr += `
            <div class="user-container">
        <h3>账号：<span id="userName">${data.username}</span></h3>
        <p>用户名: <span id="user-name">${data.nickname}</span></p>
        <p>用户学号: <span id="user-id">${data.studentId}</span></p>
        <p>用户专业：<span id="user-major">${data.major}</span></p>
        <p>用户班级：<span id="user-class">${data.class}</span></p>
        <p>用户邮箱: <span id="user-email">${data.email}</span></p>
        <button class="editInfo">修改信息</button>
        <button class="deleteUser">删除用户</button>
        </div>
        `
        });
        userDetail.innerHTML = userStr
        editInfofunciton(res.data.data)
        deleteUserInfo(res.data.data)
    })
}

function userView() {
    if (userDetail.style.display === 'none') {
        userDetail.style.display = 'block'
    } else {
        userDetail.style.display = 'none'
    }
}

function editInfofunciton(data) {
    const editInfoButtons = document.querySelectorAll('.editInfo');
    editInfoButtons.forEach((button, index) => {
        button.addEventListener('click', () => {
            const userData = data[index];
            populateModal(userData);
            openModal();
        });
    });
}

function populateModal(userData) {
    document.getElementById('user_id').innerText = userData.id;
    document.getElementById('username').value = userData.username;
    document.getElementById('name').value = userData.nickname;
    document.getElementById('studentID').value = userData.studentId;
    document.getElementById('major').value = userData.major;
    document.getElementById('class').value = userData.class;
    document.getElementById('email').value = userData.email;
}

function openModal() {
    myModal.style.display = 'block';
}

function saveChanges() {
    // 从输入字段获取值
    let username = document.getElementById('username').value;
    let name = document.getElementById('name').value;
    let studentID = document.getElementById('studentID').value;
    let major = document.getElementById('major').value;
    let userClass = document.getElementById('class').value;
    let email = document.getElementById('email').value;
    let id = document.getElementById('user_id').innerText

    console.log(
        username,
        name,
        studentID,
        major,
        userClass,
        email,
        id
    );

    // 发送 axios 请求以保存更改
    axios.post('/my/adminupdateuserinfo', {
        username: username,
        nickname: name,
        studentId: studentID,
        major: major,
        class: userClass,
        email: email,
        id: id
    }, {
        headers: {
            'Authorization': localStorage.getItem('token')
        }
    }).then(res => {
        // 处理响应，例如显示成功消息
        if (res.data.status === 1)
            alert(`保存更改时出错。请稍后再试。${res.data.message}`);
        if (res.data.status === 0) {
            alert('更改已成功保存！');
            location.reload();
        }


    }).catch(err => {
        // 处理任何错误，例如显示错误消息
        console.error(err);
        alert('保存更改时出错。请稍后再试。');
    });
}


//删除用户
function deleteUserInfo(data) {
    const deleteButtons = document.querySelectorAll('.deleteUser');
    deleteButtons.forEach((button, index) => {
        if (index === 0) {
            console.log(button);
        }
        button.addEventListener('click', () => {
            const userData = data[index];
            deleteModal(userData);
        });
    });
}

function deleteModal(userData) {
    console.log(userData);

    const isDelete = confirm(`确定要删除用户 ${userData.username} 吗？`);

    if (isDelete) {
        axios.post('/my/admindeleteuser', {
            id: userData.id
        }, {
            headers: {
                'Authorization': localStorage.getItem('token')
            }
        }).then(res => {
            if (res.data.status === 1) {
                alert(`删除用户时出错。请稍后再试。${res.data.message}`);
            }
            if (res.data.status === 0) {
                alert('用户已成功删除！');
                location.reload();
            }
        }).catch(err => {
            console.error(err);
            alert('删除用户时出错。请稍后再试。');
        });
    }


}





